package com.outsourcing.bottle.ui.fragment;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.adapter.BTFriendListAdapter;
import com.outsourcing.bottle.adapter.GroupFriendAdapter;
import com.outsourcing.bottle.adapter.MaybeKnowFriendListAdapter;
import com.outsourcing.bottle.adapter.MyFriendListAdapter;
import com.outsourcing.bottle.db.BottleTypeTable;
import com.outsourcing.bottle.db.LanguageConfigTable;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.db.PushNoticeInfoTable;
import com.outsourcing.bottle.domain.BottleFriendEntry;
import com.outsourcing.bottle.domain.BottleFriendInfo;
import com.outsourcing.bottle.domain.BottleTypeEntry;
import com.outsourcing.bottle.domain.GroupFriendEntry;
import com.outsourcing.bottle.domain.LanguageConfig;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.domain.MaybeFriendEntry;
import com.outsourcing.bottle.domain.MaybeKnowFriendInfo;
import com.outsourcing.bottle.domain.MyFriendEntry;
import com.outsourcing.bottle.domain.MyFriendInfo;
import com.outsourcing.bottle.domain.PushNoticeInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.AlbumsActivity;
import com.outsourcing.bottle.ui.BindInviteActivity;
import com.outsourcing.bottle.ui.ChooseBottleActivity;
import com.outsourcing.bottle.ui.ChooseLocationTabsActivity;
import com.outsourcing.bottle.ui.ExchangeInfoDetailActivity;
import com.outsourcing.bottle.ui.ExchangePicDetailActivity;
import com.outsourcing.bottle.ui.ExpandEditActivity;
import com.outsourcing.bottle.ui.HomeActivity;
import com.outsourcing.bottle.ui.ManagerGroupActivity;
import com.outsourcing.bottle.ui.SettingActivity;
import com.outsourcing.bottle.ui.ThrowBottleSettingActivity;
import com.outsourcing.bottle.ui.UserSpaceActivity;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ServiceUtils;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.util.UICore;
import com.outsourcing.bottle.widget.CustomListView;

/**
 * 
 * @author 06peng
 * 
 */
public class FriendFragment extends Fragment implements BasicUIEvent, Callback,
		OnClickListener, OnScrollListener {

	private final int user_baseinfo_notfull = 100;
	private final int complete_info = 1;
	
	private static final String TAG = FriendFragment.class.getSimpleName();

	private static final int EXEU_GET_BTFRIEND_LIST = 0;
	private static final int EXEU_GET_BTFRIEND_LIST_FAILED = 1;
	public static final int EXEU_GET_MAYBEKONW_LIST = 2;
	private static final int EXEU_GET_MAYBEKONW_LIST_FAILED = 3;
	public static final int EXEU_GET_MYFRIEND_LIST = 4;
	private static final int EXEU_GET_MYFRIEND_LIST_FAILED = 5;
	private static final int EXEU_GET_BOTTLE_PRIV_OPERATION = 6;
	private static final int EXEU_SEND_BOTTLE_PRIV_SUCCESS = 7;
	private static final int EXEU_SEND_BOTTLE_PRIV_SUCCESS_NOT_ENABLE = 8;
	private static final int EXEU_SEND_BOTTLE_PRIV_FAILED = 9;
	private static final int EXEU_GET_BTFRIEND_TIMELINE_MORE = 10;
	private static final int EXEU_GOTO_TA_ALBUM = 11;
	private static final int EXEU_GOTO_EXCHANGE = 12;
	private static final int INIT_HAS_EXS_ERROR = 13;
	private static final int INIT_HAS_EXS_PROFILE = 14;
	private static final int INIT_HAS_EXS_PHOTO = 15;
	private static final int EXEU_GOTO_PROFILE = 16;
	private static final int EXEU_GOTO_PHOTO = 17;
	private static final int EXEU_GET_BTFRIEND_TIMELINE_NOTHING = 18;
	public static final int EXEU_GET_MAYBEKONW_LIST_MORE = 19;
	public static final int EXEU_GET_MYFRIEND_LIST_MORE = 20;

	private static final int EXEU_CHECK_FRIEND_FAILED = 21;
	private static final int EXEU_CAN_NOT_ADD_FRIEND = 22;
	private static final int EXEU_CHECK_FRIEND_SUCCESS = 23;
	private static final int EXEU_CHECK_FRIEND = 24;

	private static final int EXEU_ALLOW_ADD_FRIEND = 25;
	
	private final int throw_bottle_init_location = 26;
	private static final int EXEU_PUSH_NOTICE = 35;
	private static final int EXEU_GET_MAYBEKONW_LIST_NODATA = 36;
	private static final int EXEU_GET_BTFRIEND_LIST_NO_DATA = 37;
	private static final int EXEU_GET_MYFRIEND_LIST_NO_DATA = 38;

	private Handler handler = null;
	private BottleFriendInfo bottleFriendInfo = null;
	private MyFriendInfo myfriendInfo = null;
	private FrameLayout mContentView = null;
	private CustomListView listView = null;
	private View loadMoreView = null;
	private View mheaderView;
	private int page = 1;
	private HomeActivity activity;

	private BTFriendListAdapter mBtfriendAdapter = null;
	private MaybeKnowFriendListAdapter maybeFriendAdapter;
	private GroupFriendAdapter groupAdapter = null;
	private List<BottleFriendEntry> mBtFriendList = null;
	private List<MaybeFriendEntry> maybeFriendEntries = null;
	private List<MyFriendEntry> myFriendEntries = null;
	private List<GroupFriendEntry> groups_list = null;
	public String[] groupNames;
	public int groupItem;
	private int visibleLastIndex = 0; // 最后的可视项索引
	private int visibleItemCount; // 当前窗口可见项总数
	/** 记录当前页面是在哪个分类 */
	private int state;
	
	private int adapter_type; //listadapter 的类型
	
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public boolean init = false;
	private int btId;
	private int btTypeId;
	private int ouid;
	private int groupid = -1;
	/**
	 * AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE/AppContext.
	 * CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE
	 */
	private int exsType;

	private MyFriendListAdapter myFriendAdapter;

	private PopupWindow popupWindow;

	private ListView list_Contact;
	private RelativeLayout mNoDataTipsLayout;
	private TextView mNoDataTipsText1;
	private TextView mNoDataTipsText2;
	private TextView mNoDataTipsButton;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ServiceUtils.dout(TAG + " onCreate");
		setRetainInstance(true);
		activity = (HomeActivity) getActivity();
		handler = new Handler(this);
//		doGetBTfriendList();
//		new Thread() {
//			public void run() {
//				getBottleFriendList(page);
//				init = true;
//			};
//		}.start();
		if (AppContext.getInstance().isGotoMaybeKown()) {
			doMenuItemSelect(1);
			AppContext.getInstance().setGotoMaybeKown(false);
		} else if (AppContext.getInstance().isGotoMyfriend()) {
			doMenuItemSelect(0);
			AppContext.getInstance().setGoToMyfriend(false);
		} else {
			if (!init) {
				UICore.eventTask(this, activity, EXEU_GET_BTFRIEND_LIST, "EXEU_GET_BTFRIEND_LIST", null);
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ServiceUtils.dout(TAG + " onCreateView");
		View view = inflater.inflate(R.layout.root_layout, null);
		mContentView = (FrameLayout) view.findViewById(R.id.mainView);
		if (!init) {
			listView = (CustomListView) LayoutInflater.from(activity).inflate(R.layout.main_refresh_list, null);
			loadMoreView = inflater.inflate(R.layout.refreshable_list_footer, null);
			loadMoreView.setVisibility(View.GONE);
			listView.addFooterView(loadMoreView);
			listView.setOnScrollListener(this);
			
			
			mheaderView = LayoutInflater.from(activity).inflate(
					R.layout.friend_list_header, null);
			mNoDataTipsLayout = (RelativeLayout)mheaderView.findViewById(R.id.rl_nodata_tip);
			mNoDataTipsText1 = (TextView)mheaderView.findViewById(R.id.tv_nodata_tips);
			mNoDataTipsText2 = (TextView)mheaderView.findViewById(R.id.tv_nodata_tips_2);
			mNoDataTipsButton = (TextView)mheaderView.findViewById(R.id.bt_nodata_try);
			listView.addHeaderView(mheaderView);
		}
		
		listView.setonRefreshListener(new CustomListView.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				page = 1;
				if (state == EXEU_GET_BTFRIEND_LIST || state == EXEU_GET_BTFRIEND_TIMELINE_MORE) {
					UICore.eventTask(FriendFragment.this, activity, EXEU_GET_BTFRIEND_LIST, null, null);	
				}
				if (state == EXEU_GET_MAYBEKONW_LIST || state == EXEU_GET_MAYBEKONW_LIST_MORE) {
					UICore.eventTask(FriendFragment.this, activity, EXEU_GET_MAYBEKONW_LIST, null, null);	
				}
				if(state == EXEU_GET_MYFRIEND_LIST || state == EXEU_GET_MYFRIEND_LIST_MORE){
					UICore.eventTask(FriendFragment.this, activity, EXEU_GET_MYFRIEND_LIST, null, null);	
				}
			}
		});

		initPopupWindow();
		return view;
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onPause() {
		super.onPause();
		ServiceUtils.dout(TAG + " onPause");
//		if (null != mContentView) {
//			mContentView.removeAllViews();
//		}
	}

	@Override
	public void onResume() {
		super.onResume();
		ServiceUtils.dout(TAG + " onResume");
//		if (init) {
//			if (state==EXEU_GET_BTFRIEND_LIST||state == EXEU_GET_BTFRIEND_TIMELINE_MORE) {
//				Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_LIST);
//				handler.sendMessage(message);
//			}
//			if (state == EXEU_GET_MAYBEKONW_LIST||state == EXEU_GET_MAYBEKONW_LIST_MORE) {
//				Message message = handler.obtainMessage(EXEU_GET_MAYBEKONW_LIST);
//				handler.sendMessage(message);
//			}
//			if (state == EXEU_GET_MYFRIEND_LIST||state == EXEU_GET_MYFRIEND_LIST_MORE) {
//				Message message = handler.obtainMessage(EXEU_GET_MYFRIEND_LIST);
//				handler.sendMessage(message);
//			}
//		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		ServiceUtils.dout(TAG + " onDestory");
	}

	@Override
	public boolean handleMessage(Message msg) {
		LanguageConfigTable languageConfigTable = null;
		LanguageConfig languageCon = null;
		try {
			setHeaderVisible();
			switch (msg.what) {
			case EXEU_GET_BTFRIEND_LIST:
				adapter_type = EXEU_GET_BTFRIEND_LIST;
				mNoDataTipsLayout.setVisibility(View.GONE);
				state = EXEU_GET_BTFRIEND_LIST;
				mContentView.removeAllViews();
				
				if (null!=mBtFriendList&&mBtFriendList.size()<10) {
					loadMoreView.setVisibility(View.GONE);
				}
				mBtfriendAdapter = new BTFriendListAdapter(activity, this, mBtFriendList);
				listView.setAdapter(mBtfriendAdapter);
				mContentView.addView(listView);
				listView.onRefreshComplete();
				init = true;
				new Thread() {
					public void run() {
						getPushNotice();
					};
				}.start();
				break;
			case EXEU_GET_BTFRIEND_TIMELINE_MORE:
				state = EXEU_GET_BTFRIEND_TIMELINE_MORE;
				mBtfriendAdapter.notifyDataSetChanged();
				listView.setSelection(visibleLastIndex - visibleItemCount + 1); // 设置选中项
				break;
			case EXEU_GET_BTFRIEND_TIMELINE_NOTHING:
				loadMoreView.setVisibility(View.GONE);
				listView.setSelection(visibleLastIndex - visibleItemCount);
				break;
			case EXEU_SEND_BOTTLE_PRIV_SUCCESS:
				final Bundle bundle = msg.getData();
				try {
					BottleTypeTable table = new BottleTypeTable();
					BottleTypeEntry bottleEntry = table.getBottleType(bundle.getInt("type"));
					int locationSelect = bottleEntry.getBttype_location_select();
					String[] choices = new String[2];
					if (locationSelect > 1) {
						choices[0] = getString(R.string.choose_location_here_throw);
						choices[1] = getString(R.string.choose_location_search_throw);
					} else {
						choices = new String[1];
						choices[0] = getString(R.string.choose_location_here_throw);
					}
					chooseAction(choices, locationSelect , bundle.getInt("type"));
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				break;
			case EXEU_PUSH_NOTICE:
				((HomeActivity) getActivity()).pushNoticeUpdateUI();
				break;
			case EXEU_SEND_BOTTLE_PRIV_SUCCESS_NOT_ENABLE:
				activity.onToast((String) msg.obj);
				break;
			case EXEU_SEND_BOTTLE_PRIV_FAILED:
				activity.onToast((String) msg.obj);
				break;
			case EXEU_GET_MAYBEKONW_LIST:
				adapter_type = EXEU_GET_MAYBEKONW_LIST;
				mNoDataTipsLayout.setVisibility(View.GONE);
				state = EXEU_GET_MAYBEKONW_LIST;
				maybeFriendAdapter = new MaybeKnowFriendListAdapter(activity, this, maybeFriendEntries);
				mContentView.removeAllViews();
				mContentView.addView(listView);
				listView.setAdapter(maybeFriendAdapter);
				
				if (maybeFriendEntries.size()<10) {
					listView.onRefreshComplete();
					loadMoreView.setVisibility(View.GONE);
				}
				listView.onRefreshComplete();
				new Thread() {
					public void run() {
						getPushNotice();
					};
				}.start();
				break;
			case EXEU_GET_MAYBEKONW_LIST_NODATA:
				state = EXEU_GET_MAYBEKONW_LIST;
				listView.setAdapter(null);
				mContentView.removeAllViews();
				mContentView.addView(listView);
				languageConfigTable = new LanguageConfigTable();
				languageCon = languageConfigTable.getBottleLanguageConfig();
				
				RelativeLayout mProfileInfo = (RelativeLayout) mheaderView
						.findViewById(R.id.rl_friend_profile);
				View line_profile_bottle = (View) mheaderView
						.findViewById(R.id.line_profile_bottle);

				LoginUserInfoTable loginUserInfoTable = new LoginUserInfoTable();
				LoginUserInfoEntry loginUserInfoEntry = loginUserInfoTable
						.getLoginUserInfo(AppContext.getInstance().getLogin_uid());

				if (null != loginUserInfoEntry
						&& loginUserInfoEntry.getBasicinfo_fullfill() == 0) {
					mProfileInfo.setVisibility(View.VISIBLE);
					line_profile_bottle.setVisibility(View.VISIBLE);
					RemoteImageView img_head = (RemoteImageView) mheaderView
							.findViewById(R.id.iv_author_photo);

					LoginUserInfoTable userInfoTable = new LoginUserInfoTable();
					if (null != userInfoTable.getLoginUserInfo(AppContext
							.getInstance().getLogin_uid())) {
						String avatar = userInfoTable.getLoginUserInfo(
								AppContext.getInstance().getLogin_uid())
								.getAvatar();
						if (!TextUtils.isEmpty(avatar)) {
							img_head.setDefaultImage(R.drawable.avatar_default_small);
							img_head.setImageUrl(avatar);
						}
					}
					ImageView img_Profile = (ImageView) mheaderView
							.findViewById(R.id.iv_profile_info);
					img_Profile.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View paramView) {
							Intent settingIntent = new Intent(activity,
									SettingActivity.class);
							startActivity(settingIntent);
						}
					});
					
					String setUpTips1 = null;
					String setUpTips2 = null;
					if (AppContext.language == 1) {
						setUpTips1 = languageCon.getWithout_maybeknow_tip_cn();
						setUpTips2 = languageCon.getWithout_maybeknow_tip_setup_cn();
					}else {
						setUpTips1 = languageCon.getWithout_maybeknow_tip_en();
						setUpTips2 = languageCon.getWithout_maybeknow_tip_setup_en();
					}
					
					
					TextView tvProfileTipTitle = (TextView) mheaderView
							.findViewById(R.id.tv_friend_profile_tips);
					tvProfileTipTitle.setText(setUpTips1);
					TextView tvProfileTips = (TextView) mheaderView
							.findViewById(R.id.tv_friend_profile_tips_2);
					tvProfileTips.setText(setUpTips2);
					Button bt_profile = (Button) mheaderView
							.findViewById(R.id.bt_friend_profile_try);
					bt_profile.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View paramView) {
							Intent settingIntent2 = new Intent(activity,
									SettingActivity.class);
							startActivity(settingIntent2);
						}
					});
				} else {
					mProfileInfo.setVisibility(View.GONE);
					line_profile_bottle.setVisibility(View.GONE);
				}
				
				mNoDataTipsLayout.setVisibility(View.VISIBLE);
				String maybeKnowTips1 = null;
				String maybeknowTips2 = null;
				if (AppContext.language == 1) {
					maybeKnowTips1 = languageCon.getWithout_maybeknow_tip_cn();
					maybeknowTips2 = languageCon.getWithout_maybeknow_tip_invite_cn();
				}else {
					maybeKnowTips1 = languageCon.getWithout_maybeknow_tip_en();
					maybeknowTips2 = languageCon.getWithout_maybeknow_tip_invite_en();
				}
				
				mNoDataTipsText1.setText(maybeKnowTips1);
				mNoDataTipsText2.setText(maybeknowTips2);
				mNoDataTipsButton.setText(TextUtil.R("friend_nodata_tip_bt_maybeknow"));
				mNoDataTipsButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View paramView) {
						Intent intent = new Intent(activity, BindInviteActivity.class);
						startActivity(intent);
					}
				});
				break;
			case EXEU_GET_BTFRIEND_LIST_NO_DATA:
				state = EXEU_GET_BTFRIEND_LIST;
				listView.setAdapter(null);
				mContentView.removeAllViews();
				mContentView.addView(listView);
				languageConfigTable = new LanguageConfigTable();
				languageCon = languageConfigTable.getBottleLanguageConfig();

				mNoDataTipsLayout.setVisibility(View.VISIBLE);
				String bottlefriendTips1 = null;
				String bottlefriendTips2 = null;
				if (AppContext.language == 1) {
					bottlefriendTips1 = languageCon.getWithout_btfriend_tip_cn();
					bottlefriendTips2 = languageCon.getWithout_btfriend_tip_2_cn();
				}else {
					bottlefriendTips1 = languageCon.getWithout_btfriend_tip_en();
					bottlefriendTips2 = languageCon.getWithout_btfriend_tip_2_en();
				}
				
				mNoDataTipsText1.setText(bottlefriendTips1);
				mNoDataTipsText2.setText(bottlefriendTips2);
				mNoDataTipsButton.setText(TextUtil.R("bottle_txt_title_gain"));
				mNoDataTipsButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View paramView) {
						Intent tryIntent2 = new Intent(activity, ChooseBottleActivity.class);
						tryIntent2.putExtra("type", "try");
						startActivity(tryIntent2);
					}
				});
				break;
			case EXEU_GET_MYFRIEND_LIST_NO_DATA:
				state = EXEU_GET_MYFRIEND_LIST;
				listView.setAdapter(null);
				mContentView.removeAllViews();
				mContentView.addView(listView);
				languageConfigTable = new LanguageConfigTable();
				languageCon = languageConfigTable.getBottleLanguageConfig();

				mNoDataTipsLayout.setVisibility(View.VISIBLE);
				String myfriendTips1 = null;
				String myfriendTips2 = null;
				if (AppContext.language == 1) {
					myfriendTips1 = languageCon.getWithout_friend_tip_cn();
					myfriendTips2 = languageCon.getWithout_friend_tip_2_cn();
				}else {
					myfriendTips1 = languageCon.getWithout_friend_tip_en();
					myfriendTips2 = languageCon.getWithout_friend_tip_2_en();
				}
				
				mNoDataTipsText1.setText(myfriendTips1);
				mNoDataTipsText2.setText(myfriendTips2);
				mNoDataTipsButton.setText(TextUtil.R("friend_nodata_tip_bt_maybeknow"));
				mNoDataTipsButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View paramView) {
						Intent intent = new Intent(activity, BindInviteActivity.class);
						startActivity(intent);
					}
				});
				break;
			case EXEU_GET_MAYBEKONW_LIST_MORE:
				state = EXEU_GET_MAYBEKONW_LIST_MORE;
				loadMoreView.setVisibility(View.GONE);
				maybeFriendAdapter.notifyDataSetChanged();
				listView.setSelection(visibleLastIndex - visibleItemCount + 1); // 设置选中项
				break;
			case EXEU_GET_MYFRIEND_LIST:
				adapter_type = EXEU_GET_MYFRIEND_LIST;
				mNoDataTipsLayout.setVisibility(View.GONE);
				state = EXEU_GET_MYFRIEND_LIST;
				myFriendAdapter = new MyFriendListAdapter(activity, this, myFriendEntries);
				mContentView.removeAllViews();
				
				if (myFriendEntries.size()<10) {
					listView.onRefreshComplete();
					loadMoreView.setVisibility(View.GONE);
				}
				listView.setAdapter(myFriendAdapter);
				listView.onRefreshComplete();
				mContentView.addView(listView);
				break;
			case EXEU_GET_MYFRIEND_LIST_MORE:
				state = EXEU_GET_MYFRIEND_LIST_MORE;
				loadMoreView.setVisibility(View.GONE);
				myFriendAdapter.notifyDataSetChanged();
				listView.setSelection(visibleLastIndex - visibleItemCount + 1); // 设置选中项
				break;
			case EXEU_GET_MAYBEKONW_LIST_FAILED:
				activity.onToast((String) msg.obj);
				break;
			case EXEU_GET_MYFRIEND_LIST_FAILED:
				activity.onToast((String) msg.obj);
				break;
			case INIT_HAS_EXS_ERROR:
				activity.onToast((String) msg.obj);
				break;
			case INIT_HAS_EXS_PHOTO:
				Intent intentPhoto = new Intent(activity, ExpandEditActivity.class);
				Bundle bundlePhoto = new Bundle();
				bundlePhoto.putString("from_type", AppContext.APPLY_PICEX);
				bundlePhoto.putInt("ouid", ouid);
				intentPhoto.putExtras(bundlePhoto);
				startActivity(intentPhoto);
				break;
			case INIT_HAS_EXS_PROFILE:
				Intent intentProfile = new Intent(activity, ExpandEditActivity.class);
				Bundle bundleProfile = new Bundle();
				bundleProfile.putString("from_type", AppContext.REPLY_INFOEX);
				bundleProfile.putInt("ouid", ouid);
				bundleProfile.putString("reply_infoex_type", "apply");
				intentProfile.putExtras(bundleProfile);
				startActivity(intentProfile);
				break;
			case EXEU_GOTO_PROFILE:
				Intent intentExchangePro = new Intent(activity, ExchangeInfoDetailActivity.class);
				intentExchangePro.putExtra("ouid", ouid);
				intentExchangePro.putExtra("page", 1);
				activity.startActivity(intentExchangePro);
				break;
			case EXEU_GOTO_PHOTO:
				Intent intentExchangePic = new Intent(activity, ExchangePicDetailActivity.class);
				intentExchangePic.putExtra("ouid", ouid);
				intentExchangePic.putExtra("page", 1);
				activity.startActivity(intentExchangePic);
				break;
			case EXEU_CHECK_FRIEND_FAILED:
				activity.onToast((String) msg.obj);
				break;
			case EXEU_CAN_NOT_ADD_FRIEND:
				activity.onToast((String) msg.obj);
				break;
			case EXEU_CHECK_FRIEND_SUCCESS:
				activity.onToast((String) msg.obj);
				Intent userIntent = new Intent(activity, UserSpaceActivity.class);
				userIntent.putExtra("ouid", ouid);
				startActivity(userIntent);
				break;
			case EXEU_ALLOW_ADD_FRIEND:
				Intent applyIntent = new Intent(activity, ExpandEditActivity.class);
				Bundle bundleFriend = new Bundle();
				bundleFriend.putString("from_type", AppContext.APPLY_FRIEND);
				bundleFriend.putInt("ouid", ouid);
				applyIntent.putExtras(bundleFriend);
				startActivity(applyIntent);
				break;
			case user_baseinfo_notfull:
				LoginUserInfoTable userTable = new LoginUserInfoTable();
				LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
				activity.showChoseMes(userEntry.getBasicinfo_tip(), complete_info);
				break;
			case throw_bottle_init_location:
//				try {
//					int bottleid = Integer.parseInt(msg.obj.toString());
//					Intent intent2 = new Intent(activity, ThrowBottleSettingActivity.class);
//					intent2.putExtra("bttype_id", bottleid);
//					intent2.putExtra("bt_geo_lng", longitude);
//					intent2.putExtra("bt_geo_lat", latitude);
//					startActivity(intent2);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private void setHeaderVisible(){
		RelativeLayout mProfileInfo = (RelativeLayout) mheaderView
				.findViewById(R.id.rl_friend_profile);
		View line_profile_bottle = (View)mheaderView.findViewById(R.id.line_profile_bottle);
		
		LoginUserInfoTable loginUserInfoTable = new LoginUserInfoTable();
		LoginUserInfoEntry loginUserInfoEntry = loginUserInfoTable
				.getLoginUserInfo(AppContext.getInstance().getLogin_uid());

		if (null != loginUserInfoEntry
				&& loginUserInfoEntry.getBasicinfo_fullfill() == 0) {
			mProfileInfo.setVisibility(View.VISIBLE);
			line_profile_bottle.setVisibility(View.VISIBLE);
			RemoteImageView img_head = (RemoteImageView) mheaderView
					.findViewById(R.id.iv_author_photo);

			LoginUserInfoTable userInfoTable = new LoginUserInfoTable();
			if (null != userInfoTable.getLoginUserInfo(AppContext
					.getInstance().getLogin_uid())) {
				String avatar = userInfoTable.getLoginUserInfo(
						AppContext.getInstance().getLogin_uid())
						.getAvatar();
				if (!TextUtils.isEmpty(avatar)) {
					img_head.setDefaultImage(R.drawable.avatar_default_small);
					img_head.setImageUrl(avatar);
				}
			}
			ImageView img_Profile = (ImageView) mheaderView
					.findViewById(R.id.iv_profile_info);
			img_Profile.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View paramView) {
					Intent settingIntent = new Intent(activity, SettingActivity.class);
					startActivity(settingIntent);
				}
			});
			TextView tvProfileTips = (TextView) mheaderView
					.findViewById(R.id.tv_friend_profile_tips_2);
			tvProfileTips.setText(loginUserInfoEntry.getBasicinfo_tip());
			Button bt_profile = (Button) mheaderView
					.findViewById(R.id.bt_friend_profile_try);
			bt_profile.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View paramView) {
					Intent settingIntent2 = new Intent(activity, SettingActivity.class);
					startActivity(settingIntent2);
				}
			});
		} else {
			mProfileInfo.setVisibility(View.GONE);
			line_profile_bottle.setVisibility(View.GONE);
		}
		
	}
	
	@Override
	public void execute(int mes, Object obj) {
		adapter_type = mes;
		switch (mes) {
		case EXEU_GET_BTFRIEND_LIST:
			getBottleFriendList(page);
			init = true;
			break;
		case EXEU_GET_BTFRIEND_TIMELINE_MORE:
			getMoreBottleFriendList(page);
			break;
		case EXEU_GET_BOTTLE_PRIV_OPERATION:
			getSendSingleBtPriv(btTypeId, btId);
			break;
		case EXEU_GET_MAYBEKONW_LIST:
			getMaybeknowFriendsList(page);
			break;
		case EXEU_GET_MAYBEKONW_LIST_MORE:
			getMoreMaybeknowFriendsList(page);
			break;
		case EXEU_GET_MYFRIEND_LIST:
			getFriendsList(page, groupid, AppContext.getInstance().getLogin_uid());
			break;
		case EXEU_GET_MYFRIEND_LIST_MORE:
			getMoreFriendsList(page, groupid, AppContext.getInstance().getLogin_uid());
			break;
		case EXEU_GOTO_TA_ALBUM:
			checkVisible(ouid);
			break;
		case EXEU_GOTO_EXCHANGE:
			getHasExs(exsType, ouid);
			break;
		case EXEU_CHECK_FRIEND:
			CheckFriendApply(ouid);
			break;
		case throw_bottle_init_location:
			Object idObj = obj;
			((BasicActivity) getActivity()).networkLocat();
			Message msg = Message.obtain(handler, throw_bottle_init_location, idObj);
			handler.sendMessage(msg);
			break;
		default:
			break;
		}
	}

	private void getMoreBottleFriendList(int page2) {
		StringBuffer url = new StringBuffer(UrlConfig.get_btfs_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getMoreBottleFriendList url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getMoreBottleFriendList results:" + results.toString());
			Gson gson = new Gson();
			BottleFriendInfo bottleFriendInfo = gson.fromJson(results,
					BottleFriendInfo.class);
			if (bottleFriendInfo.getResults().getAuthok() == 1) {
				List<BottleFriendEntry> mMoreBtFriendList = bottleFriendInfo.getBtfs_list();
				if (null != mMoreBtFriendList && mMoreBtFriendList.size() > 0) {
					mBtFriendList.addAll(mMoreBtFriendList);
					Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_TIMELINE_MORE);
					handler.sendMessage(message);
				} else {
					Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_TIMELINE_NOTHING);
					handler.sendMessage(message);
				}
			} else {
				Message message = handler.obtainMessage(
						EXEU_GET_BTFRIEND_LIST_FAILED, bottleFriendInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doVisitAlbum(int ouid) {
		this.ouid = ouid;
		UICore.eventTask(this, activity, EXEU_GOTO_TA_ALBUM, "", null);
	}

	public void doExchange(int exsType, int ouid) {
		if (exsType == AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE) {
			LoginUserInfoTable userTable = new LoginUserInfoTable();
			LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
			if (userEntry.getBasicinfo_fullfill() == 0) {
				handler.sendEmptyMessage(user_baseinfo_notfull);
			} else {
				this.ouid = ouid;
				this.exsType = exsType;
				UICore.eventTask(this, activity, EXEU_GOTO_EXCHANGE, "has_exs", null);
			}	
		}else {
			this.ouid = ouid;
			this.exsType = exsType;
			UICore.eventTask(this, activity, EXEU_GOTO_EXCHANGE, "has_exs", null);
		}
		
	}

	private void getHasExs(int exsType, int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.get_has_exs);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		ServiceUtils.dout(TAG + " getHasExs url:" + url.toString());
		try {
			String result = HttpUtils.doGet(url.toString());
			ServiceUtils.dout(TAG + " getHasExs result:" + result.toString());
			if (result != null) {
				JSONObject object = new JSONObject(result);
				JSONObject resultObj = object.getJSONObject("results");
				int authok = resultObj.getInt("authok");
				if (authok == 0) {
					String errmsg = resultObj.getString("errmsg");
					Message msg = Message.obtain(handler, INIT_HAS_EXS_ERROR, errmsg);
					handler.sendMessage(msg);
				} else {
					JSONObject hasExsObj = object.getJSONObject("has_exs");
					if (exsType == AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE) {
						int has_infoexs = hasExsObj.getInt("has_infoexs");
						if (has_infoexs == 1) {
							handler.sendEmptyMessage(EXEU_GOTO_PROFILE);
						} else {
							handler.sendEmptyMessage(INIT_HAS_EXS_PROFILE);
						}
					} else if (exsType == AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE) {
						int has_picexs = hasExsObj.getInt("has_picexs");
						if (has_picexs == 1) {
							handler.sendEmptyMessage(EXEU_GOTO_PHOTO);
						} else {
							handler.sendEmptyMessage(INIT_HAS_EXS_PHOTO);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 相册是否可见 */
	private void checkVisible(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.check_ouser_visible);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject object = new JSONObject(result);
				JSONObject resultObj = object.getJSONObject("results");
				if (resultObj.getInt("authok") == 1) {
					JSONObject visibleObj = object.getJSONObject("visible_info");
					int album_visible = visibleObj.getInt("album_visible");
					if (album_visible == 1) {
						Intent intent = new Intent(activity, AlbumsActivity.class);
						intent.putExtra("ouid", ouid);
						startActivity(intent);
					} else {
						activity.onToast(getString(R.string.exchange_feed_invisible_tips));
					}
				} else {
					activity.onToast(getString(R.string.exchange_feed_invisible_tips));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 获取扔瓶子的权限 */
	public void getSendSingleBtPriv(int btTypeId, int btId) {
		StringBuffer url = new StringBuffer(UrlConfig.get_sendsinglebt_priv);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&bttypeid=" + btTypeId);
		ServiceUtils.dout("sendsingleBT private url:" + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("getsendsinglebt_priv result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int authok = resultObj.getInt("authok");
				String errmsg = resultObj.getString("errmsg");
				if (authok == 1) {
					JSONObject privObj = obj.getJSONObject("sendsinglebt_priv");
					int enable = privObj.getInt("enable");
					String reason = privObj.getString("reason");
					if (enable == 1) {
						Bundle bundle = new Bundle();
						bundle.putInt("type", btTypeId);
						bundle.putInt("btid", btId);
						bundle.putString("reason", reason);
						Message message = handler.obtainMessage(EXEU_SEND_BOTTLE_PRIV_SUCCESS);
						message.setData(bundle);
						handler.sendMessage(message);
					} else {
						Message message = handler.obtainMessage(EXEU_SEND_BOTTLE_PRIV_SUCCESS_NOT_ENABLE, reason);
						handler.sendMessage(message);
					}

				} else {
					Message message = handler.obtainMessage(EXEU_SEND_BOTTLE_PRIV_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void doSendSingleBtPri(int btTypeId, int btId) {
		this.btId = btId;
		this.btTypeId = btTypeId;
		UICore.eventTask(this, activity, EXEU_GET_BOTTLE_PRIV_OPERATION, "", null);
	}

	public void doGetBTfriendList_Refresh() {
		UICore.eventTask(this, activity, EXEU_GET_BTFRIEND_LIST, "EXEU_GET_BTFRIEND_LIST", null);
	}

	public void doMenuItemSelect(int which) {
		page = 1;

		switch (which) {
		case 0:// 好友
			if (myFriendEntries != null && myFriendEntries.size() > 0) {
				Message message = handler.obtainMessage(EXEU_GET_MYFRIEND_LIST);
				handler.sendMessage(message);
			} else {
				UICore.eventTask(this, activity, EXEU_GET_MYFRIEND_LIST, "", null);
			}
			break;
		case 1:// 可能认识
			if (maybeFriendEntries != null && maybeFriendEntries.size() > 0) {
				Message message = handler.obtainMessage(EXEU_GET_MAYBEKONW_LIST);
				handler.sendMessage(message);
			} else {
				UICore.eventTask(this, activity, EXEU_GET_MAYBEKONW_LIST, "", null);
			}
			break;
		default:
			break;
		}
	}
	
	public void doMenuItemSelect2(int which) {
		page = 1;

		switch (which) {
		case 0:// 好友
			if (myFriendEntries != null && myFriendEntries.size() > 0) {
				Message message = handler.obtainMessage(EXEU_GET_MYFRIEND_LIST);
				handler.sendMessage(message);
			} else {
				UICore.eventTask(this, activity, EXEU_GET_MYFRIEND_LIST, "", null);
			}
			break;
		case 1:// 瓶友
			if (mBtFriendList != null && mBtFriendList.size() > 0) {
				Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_LIST);
				handler.sendMessage(message);
			} else {
				UICore.eventTask(this, activity, EXEU_GET_BTFRIEND_LIST, "", null);
			}
			break;
		default:
			break;
		}
	}

	private void getBottleFriendList(int page) {
		if (AppContext.getInstance().getLogin_uid() == 0
				|| AppContext.getInstance().getLogin_token() == null) {
			initConfig();
		}
		StringBuffer url = new StringBuffer(UrlConfig.get_btfs_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getBottleFriendList url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getFeedLocationList results:" + results.toString());
			Gson gson = new Gson();
			bottleFriendInfo = gson.fromJson(results, BottleFriendInfo.class);
			if (bottleFriendInfo.getResults().getAuthok() == 1) {
				mBtFriendList = bottleFriendInfo.getBtfs_list();
				if (null != mBtFriendList && mBtFriendList.size()>0) {
					Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_LIST);
					handler.sendMessage(message);	
				}else {
					Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_LIST_NO_DATA);
					handler.sendMessage(message);
				}
			} else {
				Message message = handler.obtainMessage(
						EXEU_GET_BTFRIEND_LIST_FAILED, bottleFriendInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getPushNotice() {
		StringBuffer url = new StringBuffer(UrlConfig.push_notice);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject obj = new JSONObject(result);
				JSONObject resultObj = obj.getJSONObject("results");
				if (resultObj.getInt("authok") == 1) {
					JSONObject pushObj = obj.getJSONObject("push_notice");
					PushNoticeInfoTable table = new PushNoticeInfoTable();
					PushNoticeInfo pushInfo = new PushNoticeInfo();
					table.clearTable();
					pushInfo.setMessage_avatar(pushObj.getString("message_avatar"));
					pushInfo.setMessage_content(pushObj.getString("message_content"));
					pushInfo.setNew_btfeed_count(pushObj.getInt("new_btfeed_count"));
					pushInfo.setNew_exfeed_count(pushObj.getInt("new_exfeed_count"));
					pushInfo.setNew_message_count(pushObj.getInt("new_message_count"));
					pushInfo.setNew_notice_count(pushObj.getInt("new_notice_count"));
					pushInfo.setNew_maybeknow_count(pushObj.getInt("new_maybeknow_count"));
					table.savePushNoticeInfo(pushInfo, AppContext.getInstance().getLogin_uid());
					
					Message message = Message.obtain(handler);
					message.what = EXEU_PUSH_NOTICE;
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 获取好友列表
	 * 
	 * @param page
	 */
	private void getFriendsList(int page, int groupid, int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.get_friends_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&groupid=" + groupid);
		url.append("&ouid=" + ouid);
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getFriendsList url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());

			if (results == null) {
				return;
			}
			ServiceUtils.dout("getFriendsList results:" + results.toString());
			Gson gson = new Gson();
			myfriendInfo = gson.fromJson(results, MyFriendInfo.class);
			if (myfriendInfo.getResults().getAuthok() == 1) {
				groups_list = myfriendInfo.getGroups_list();
				if (groups_list != null && !groups_list.isEmpty()) {
					groupNames = new String[groups_list.size() + 3];
					groupNames[0] = getString(R.string.friend_btfriend);
					groupNames[1] = getString(R.string.friend_maybe_kown);
					groupNames[2] = getString(R.string.friend_all_friend);
					
					for (int i = 3;i < groups_list.size() + 3;i++) {
						groupNames[i] = groups_list.get(i - 3).getGroupname();
					}
					groupNames[2+groups_list.size()] = getString(R.string.friend_mangager_group);
				}
				myFriendEntries = myfriendInfo.getFriends_list();
				if (myFriendEntries.isEmpty()) {
					Message message = handler.obtainMessage(EXEU_GET_MYFRIEND_LIST_NO_DATA);
					handler.sendMessage(message);
				}else {
					Message message = handler.obtainMessage(EXEU_GET_MYFRIEND_LIST);
					handler.sendMessage(message);
				}
				
			} else {
				Message message = handler.obtainMessage(
						EXEU_GET_MYFRIEND_LIST_FAILED, myfriendInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 获取更多好友列表
	 * 
	 * @param page
	 */
	private void getMoreFriendsList(int page, int groupid, int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.get_friends_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&groupid=" + groupid);
		url.append("&ouid=" + ouid);
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getMoreFriendsList url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getMoreFriendsList results:" + results.toString());
			Gson gson = new Gson();
			myfriendInfo = gson.fromJson(results, MyFriendInfo.class);
			if (myfriendInfo.getResults().getAuthok() == 1) {
				List<MyFriendEntry> moreEntries = myfriendInfo.getFriends_list();
				if (null != moreEntries && moreEntries.size() > 0) {
					myFriendEntries.addAll(moreEntries);
					Message message = handler.obtainMessage(EXEU_GET_MYFRIEND_LIST_MORE);
					handler.sendMessage(message);
				} else {
					Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_TIMELINE_NOTHING);
					handler.sendMessage(message);
				}

			} else {
				Message message = handler.obtainMessage(
						EXEU_GET_MYFRIEND_LIST_FAILED, myfriendInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 获取可能认识好友列表
	 * 
	 * @param page
	 */
	private void getMaybeknowFriendsList(int page) {
		StringBuffer url = new StringBuffer(UrlConfig.get_maybeknows_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getMaybeknowFriendsList url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getMaybeknowFriendsList results:"
					+ results.toString());
			Gson gson = new Gson();
			MaybeKnowFriendInfo maybefriendInfo = gson.fromJson(results, MaybeKnowFriendInfo.class);
			if (maybefriendInfo.getResults().getAuthok() == 1) {
				maybeFriendEntries = maybefriendInfo.getMaybeknows_list();
				if (null != maybeFriendEntries && maybeFriendEntries.size()>0) {
					Message message = handler.obtainMessage(EXEU_GET_MAYBEKONW_LIST);
					handler.sendMessage(message);	
				}else {
					Message message = handler.obtainMessage(EXEU_GET_MAYBEKONW_LIST_NODATA);
					handler.sendMessage(message);	
				}
			} else {
				Message message = handler.obtainMessage(
						EXEU_GET_MAYBEKONW_LIST_FAILED, bottleFriendInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 获取可能认识好友列表
	 * 
	 * @param page
	 */
	private void getMoreMaybeknowFriendsList(int page) {
		StringBuffer url = new StringBuffer(UrlConfig.get_maybeknows_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getMoreMaybeknowFriendsList url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getMoreMaybeknowFriendsList results:" + results.toString());
			Gson gson = new Gson();
			MaybeKnowFriendInfo maybefriendInfo = gson.fromJson(results, MaybeKnowFriendInfo.class);
			if (maybefriendInfo.getResults().getAuthok() == 1) {
				List<MaybeFriendEntry> moreMaybeFriendEntries = maybefriendInfo.getMaybeknows_list();
				if (null != moreMaybeFriendEntries
						&& moreMaybeFriendEntries.size() > 0) {
					maybeFriendEntries.addAll(moreMaybeFriendEntries);
					Message message = handler.obtainMessage(EXEU_GET_MAYBEKONW_LIST_MORE);
					handler.sendMessage(message);
				} else {
					Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_TIMELINE_NOTHING);
					handler.sendMessage(message);
				}
			} else {
				Message message = handler.obtainMessage(
						EXEU_GET_MAYBEKONW_LIST_FAILED, bottleFriendInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initPopupWindow() {
		View popview = LayoutInflater.from(activity).inflate(
				R.layout.contact_popmeun, null);
		popupWindow = new PopupWindow(popview, 200, LayoutParams.WRAP_CONTENT);
		// mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bubble_frame_pop));
		popupWindow.setOutsideTouchable(true);
		// 自定义动画
//		popupWindow.setAnimationStyle(R.style.map_menu_animation);
		// 使用系统动画
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
		popupWindow.update();
		popupWindow.setTouchable(true);
		popupWindow.setFocusable(true);

		list_Contact = (ListView) popview.findViewById(R.id.list_contact);

	}

	public void showPopupWindow(View view) {
		groupAdapter = new GroupFriendAdapter(this, activity, groups_list);
		list_Contact.setAdapter(groupAdapter);
		if (!popupWindow.isShowing()) {
			popupWindow.showAsDropDown(view, 0, 0);
			
		} else {
			popupWindow.dismiss();
		}
	}
	
	public void doSelectGroupAction() {
		final Context dialogContext = new ContextThemeWrapper(activity, android.R.style.Theme_Light);
		String cancel = getString(R.string.bt_title_back);
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, groupNames);
	
		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(getString(R.string.choose_friend_select_group));
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						groupItem = which;
						activity.titleView.setText(groupNames[groupItem]);
						if (groupItem == 0) {
							if (mBtFriendList != null && mBtFriendList.size() > 0) {
								Message message = handler.obtainMessage(EXEU_GET_BTFRIEND_LIST);
								handler.sendMessage(message);
							} else {
								UICore.eventTask(FriendFragment.this, activity, EXEU_GET_BTFRIEND_LIST, "", null);
							}
						} else if (groupItem == 1){
							if (maybeFriendEntries != null && maybeFriendEntries.size() > 0) {
								Message message = handler.obtainMessage(EXEU_GET_MAYBEKONW_LIST);
								handler.sendMessage(message);
							} else {
								UICore.eventTask(FriendFragment.this, activity, EXEU_GET_MAYBEKONW_LIST, "", null);
							}
						} else if (groupItem == 2) {
							groupid = -1;
							UICore.eventTask(FriendFragment.this, activity, EXEU_GET_MYFRIEND_LIST, "", null);
						} else if (groupItem == groupNames.length-1) {
							activity.titleView.setText("");
							Intent intent = new Intent(activity, ManagerGroupActivity.class);
							activity.startActivity(intent);
						} else {
							groupid = groups_list.get(which - 3).getGroupid();
							UICore.eventTask(FriendFragment.this, activity, EXEU_GET_MYFRIEND_LIST, "", null);
						}
					}
				});
		builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
			
		});
		builder.create().show();
	}

	public void doCheckApplyFriend(int ouid){
		this.ouid = ouid;
		UICore.eventTask(this, activity, EXEU_CHECK_FRIEND, "", null);
	}
	/***
	 * 好友申请前检测接口
	 */
	private void CheckFriendApply(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.check_friendapply);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		ServiceUtils.dout("CheckFriendApply url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());

			ServiceUtils.dout("CheckFriendApply results:" + results.toString());
			
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int authok = resultObj.getInt("authok");
				String errmsg = resultObj.getString("errmsg");
				int check_result = resultObj.getInt("check_result");
				String msg = resultObj.getString("msg");
				if (authok == 1) {
					Message message = null;
					switch (check_result) {
					case 0:
						message = handler.obtainMessage(EXEU_CAN_NOT_ADD_FRIEND,msg);
						handler.sendMessage(message);
						break;
					case 1:
					    message = handler.obtainMessage(EXEU_CHECK_FRIEND_SUCCESS,msg);
						handler.sendMessage(message);	
						break;
					case 2:
						message = handler.obtainMessage(EXEU_ALLOW_ADD_FRIEND);
						handler.sendMessage(message);
						break;
					default:
						break;
					}

				} else {
					Message message = handler.obtainMessage(EXEU_CHECK_FRIEND_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		listView.setFirstVisiableItem(firstVisibleItem);
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount;

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		try {
			BaseAdapter adapter = null;
			if (adapter_type == EXEU_GET_BTFRIEND_LIST) {
				adapter = mBtfriendAdapter;
			} else if (adapter_type == EXEU_GET_MAYBEKONW_LIST) {
				adapter = maybeFriendAdapter;
			} else if (adapter_type == EXEU_GET_MYFRIEND_LIST) {
				adapter = myFriendAdapter;
			}
			if (adapter != null) {
				int itemsLastIndex = adapter.getCount() + 2; // 数据集最后一项的索引
				int lastIndex = itemsLastIndex + 1; // 加上底部的loadMoreView项 和 head项
				ServiceUtils.dout(TAG+" lastIndex:       "+lastIndex);
				ServiceUtils.dout(TAG+" visibleLastIndex:"+visibleLastIndex);
				
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
					// 如果是自动加载,可以在这里放置异步加载数据的代码
					page++;
					loadMoreView.setVisibility(View.VISIBLE);
					switch (state) {
					case EXEU_GET_BTFRIEND_LIST:
						UICore.eventTask(this, activity, EXEU_GET_BTFRIEND_TIMELINE_MORE, null, null);
						break;
					case EXEU_GET_MAYBEKONW_LIST:
						UICore.eventTask(this, activity, EXEU_GET_MAYBEKONW_LIST_MORE, null, null);
						break;
					case EXEU_GET_MYFRIEND_LIST:
						UICore.eventTask(this, activity, EXEU_GET_MYFRIEND_LIST_MORE, null, null);
						break;
					default:
						break;
					}

					Log.i("LOADMORE", "loading...");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initConfig() {
    	String country = getResources().getConfiguration().locale.getCountry();
		if (country.equals("CN")) {
			AppContext.setLanguage(1);
		} else {
			AppContext.setLanguage(0);
		}
    	
    	SharedPreferences preferences = getActivity().getSharedPreferences("Preferences_userinfo", Activity.MODE_APPEND);
    	int login_uid = preferences.getInt("login_uid", 0);
    	String login_token = preferences.getString("login_token", null);
    	AppContext.getInstance().setLogin_uid(login_uid);
		AppContext.getInstance().setLogin_token(login_token);
    }
	
	private void chooseAction(String[] choices, final int locationSelected, final int bottleId) {
		final Context dialogContext = new ContextThemeWrapper(activity, android.R.style.Theme_Light);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {
							Intent intent2 = new Intent(activity, ThrowBottleSettingActivity.class);
							intent2.putExtra("bttype_id", bottleId);
							intent2.putExtra("bt_geo_lng", AppContext.getInstance().getLongitude());
							intent2.putExtra("bt_geo_lat", AppContext.getInstance().getLatitude());
							startActivity(intent2);
						} else {
							Intent intent = new Intent(activity, ChooseLocationTabsActivity.class);
							intent.putExtra("type", "throw");
							intent.putExtra("bottleId", bottleId);
							intent.putExtra("locationSelect", locationSelected);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
}
